<extend name="Public/base" />

<block name="body"> <script type="text/javascript"
	src="__STATIC__/uploadify/jquery.uploadify.min.js"></script>

<div class="main-title">
	<h2>{:isset($info['id'])?'编辑':'新增'}广告</h2>
</div>
<div class="tab-wrap">
	<form action="__SELF__" method="post" class="form-horizontal">
		<div class="form-item">
			<label class="item-label">标题<span class="check-tips">（不能为空）</span></label>
			<div class="controls">
				<input type="text" class="text input-large" name="title" value="{$info.title}">
			</div>
		</div>
		<div class="form-item">
			<label class="item-label">位置<span class="check-tips">（不能为空）</span></label>
			<div class="controls">
				<select name="position" title='选择' id="position">
					<volist name=":getAdAdminPositions()" id='vo'>
						<option value="{$key}" <eq name="info.position" value="$key">selected="selected"</eq>>{$vo}</option>
					</volist>
				</select>
			</div>
		</div>
		<div class="form-item">
			<label class="item-label">排序<span class="check-tips">数字越小越靠前</span></label>
			<div class="controls">
				<input name="sort" value="{$info.sort|default=0}" class="text input-small" />
			</div>
		</div>
		<div class="form-item">
			<label class="item-label">广告图片<span class="check-tips"></span></label>
			<div class="controls">
				<input type="file" id="upload_picture_file">
				<input type="hidden" name="cover_id" id="cover_id" value="{$info.cover_id}" />
				<div class="upload-img-box">
					<notempty name="info.cover_id">
						<div class="upload-pre-item">
							<img src="__ROOT__{$info.cover_id|get_cover='path'}" /><span class="btn-close" title="删除图片" onclick="$(this).parent().remove();"></span>
						</div>
					</notempty>
				</div>
			</div>
			<script type="text/javascript">
			$("#upload_picture_file").uploadify({
		        "height"          : 30,
		        "swf"             : "__STATIC__/uploadify/uploadify.swf",
		        "fileObjName"     : "download",
		        "buttonText"      : "上传图片",
		        "uploader"        : "{:U('File/uploadPicture',array('session_id'=>session_id()))}",
		        "width"           : 120,
		        'removeTimeout'	  : 1,
		        'fileTypeExts'	  : '*.jpg; *.png; *.gif;',
		        "onUploadSuccess" : uploadPicture_panel,
		        'onFallback' : function() {
		            alert('未检测到兼容版本的Flash.');
		        }
		    });
			function uploadPicture_panel(file, data){
		    	var data = $.parseJSON(data);
		    	var src = '';
		        if(data.status){
		        	$("#cover_id").val(data.id);
		        	src = data.url || '__ROOT__' + data.path
		        	$("#cover_id").parent().find('.upload-img-box').html(
		        		'<div class="upload-pre-item"><img src=" ' + src + '"/></div>'
		        	);
		        } else {
		        	updateAlert(data.info);
		        	setTimeout(function(){
		                $('#top-alert').find('button').click();
		                $(that).removeClass('disabled').prop('disabled',false);
		            },1500);
		        }
		    }
			</script>
		</div>
		<div class="form-item">
			<label class="item-label">绑定商品</label>
			<div class="controls">
				<input type="text" class="text input-4x" id="product" onclick="$(this).val('');" value="{$info.product_id|get_table_field=###,'id','title','product'}" placeholder="商品名称或编号">
				<input type="hidden" name="product_id" value="{$info.product_id}">
			</div>
			<script type="text/javascript">
				$('#product').click(function(){
					var typevalue=$('#position option:selected').val();
					console.log(typevalue);
		     		var action = '{:U("Product/product_autocomplete")}?type='+typevalue;
		     		console.log(action);
		     		var input_val = $('input[name="product_id"]');
		     		var products = [];
		     		$('#product').typeahead({
		     			source: function(query, process) {
		     				var parameter = {query: query, d: new Date()};		     				
		     				$.post(action, parameter, function (data) {
		     					console.log(action);
		     					products = data;
		     					var results = $.map(products, function (pro) {
		                             return pro.id;
		                         });
		                         process(results);
		     				}, 'json');
		     			},		     			
		     			matcher: function (item) {
		     				return true;
		     			},		     			
		     			highlighter: function (item) {
		     				var pro = [];		     				
		     				$.map(products, function (u) {
		     					if (u.id == item) {
		     						pro = u;
		     					}
		     				});		     				
		     				return pro.title + " (" + pro.id + ")";
		     			},		
		     			updater: function (item) {
		     				input_val.val(item);		     				
		     				var pro = [];		     				
		     				$.map(products, function (u) {
		     					if (u.id == item) {
		     						pro = u;
		     					}
		     				});		     				
		     				return pro.title;
		     			}
		     	   });
				})
				$(function($) {
					
		     	});
			</script>
		</div>
		<div class="form-item">
    		<label class="item-label">开始时间</label>
    		<div class="controls no-overview">
	     		<input type="text" class="text input-2x" name="begin_time" id="begin_time" value="<notempty name="info.begin_time">{$info.begin_time|time_format=###,'Y-m-d'}</notempty>" placeholder="开始时间">
	     	</div>
	    </div>
	    <div class="form-item">
    		<label class="item-label">结束时间</label>
    		<div class="controls no-overview">
	     		<input type="text" class="text input-2x" name="end_time" id="end_time" value="<notempty name="info.end_time">{$info.end_time|time_format=###,'Y-m-d'}</notempty>" placeholder="结束时间">
	     	</div>
	    </div>
		<div class="form-item">
			<button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal">保存</button>
			<button class="btn btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>
		</div>
	</form>
</div>
</block>

<block name="script">
<link href="__STATIC__/datetimepicker/css/datetimepicker.css" rel="stylesheet" type="text/css">
<php>if(C('COLOR_STYLE')=='blue_color') echo '<link href="__STATIC__/datetimepicker/css/datetimepicker_blue.css" rel="stylesheet" type="text/css">';</php>
<script type="text/javascript" src="__STATIC__/datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript" src="__STATIC__/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js" charset="UTF-8"></script>
<link rel="stylesheet" type="text/css" href="__STATIC__/bootstrap/css/bootstrap-select.css" />
<link rel="stylesheet" type="text/css" href="__STATIC__/bootstrap/css/bootstrap-typeahead.css" />
<script type="text/javascript" src="__STATIC__/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript" src="__STATIC__/bootstrap/js/bootstrap-select.js"></script>
<script type="text/javascript" src="__STATIC__/bootstrap/js/bootstrap-typeahead.js"></script>
<script type="text/javascript" charset="utf-8">
highlight_subnav('{:U('Ad/index')}');
$(function(){
	//日期时间选择器
	$("#begin_time").datetimepicker({
	    format: "yyyy-mm-dd",
	    autoclose: true,
	    minView: "month",
	    maxView: "decade",
	    todayBtn: true,
	    pickerPosition: "bottom-left"
	}).on("click",function(ev){
	    $("#begin_time").datetimepicker("setEndDate", $("#end_time").val());
	});
	
	$("#end_time").datetimepicker({
	    format: "yyyy-mm-dd",
	    autoclose: true,
	    minView: "month",
	    maxView: "decade",
	    todayBtn: true,
	    pickerPosition: "bottom-left"
	}).on("click", function (ev) {
	    $("#end_time").datetimepicker("setStartDate", $("#begin_time").val());
	});
});
</script>
</block>
